import { Location, LocationStrategy, PathLocationStrategy } from '@angular/common';
import { Component, OnInit } from '@angular/core';
// import { ToastService, UploadConfig } from 'ng-tui';
import { GlobalService, AuthService } from '../../../../cores/services';
import { SaleService } from '../../services/sale.service';
import { ActivatedRoute, Router } from '@angular/router';
import { Pagination, ConfirmService, ToastService, Item } from 'ng-tui';
import { ApiData, SearchParams } from '../../../../cores/classes';
import { Goods } from '../../interfaces/manage.interface';
import { skipWhile, switchMap } from 'rxjs/operators';

@Component({
    templateUrl: './contract-manage.component.html',
    providers: [Location, { provide: LocationStrategy, useClass: PathLocationStrategy }],
})
export class ContractManageComponent implements OnInit {

    theads = ['#', '合同编号', '客户', '理货员', '业务员', '状态', '建档日期', '起始日期','截至日期', '更新日期', '回款账期', '公司抬头','结账日','交票日','付款日','信誉额度','已用额度','可用额度','是否启用','操作',];

    thead = ['#', '产品编号', '产品名称', '条形码', '对方货号', '规格型号', '计量单位','口味/颜色','规格/尺寸', '供货单位', '含税进价', '未税进价', '以箱下单', '保鲜天数', '品牌', '毛利', '毛利率', '助记符', '最低供价','零售价','最低发货基数','操作'];

    search = new SearchParams({ start: '', end: '' });

    list = [];
    detailList = [];
    loading = false;
    idDetail: any = '';
    isSelect = false;

    pagination = new Pagination();
    parentTypes = new Array<Item>();
    childTypes = new Array<any>();
    goodsThreeType = new Array<any>();
    goods = { id: 0, isActive: 1, goodsName: '',isSelect: false, goodsParentType: ''  };


    get getChildTypes(): Item[] {
        return this.childTypes.filter(type => type.parentId === this.goods.goodsParentType);
    }
    get getThreeTypes(): Item[] {
        return this.childTypes.filter(type => type.parentId === this.goods.goodsParentType);
    }
    constructor(
        private activatedRoute: ActivatedRoute,
        private router: Router,
        private confirm: ConfirmService,
        private toast: ToastService,
        public global: GlobalService,
        private manageService: SaleService
    ) { }

    ngOnInit() {
        this.activatedRoute.url
            .pipe(skipWhile(() => this.router.url !== '/sales/contract'))
            .subscribe(() => {
                this.loadDatas(() => {
                    this.idDetail = this.list[0].id;
                    this.loadDetailDatas(this.idDetail);
                });
            });
    }

    doSearch() {
        this.pagination.reset();
        this.loadDatas();
    }

    doReset() {
        this.search.clean();
        this.doSearch();
    }
    //获取合同列表
    loadDatas(complete = new Function) {
        this.loading = true;
        this.manageService.searchGoods(this.pagination, this.search)
            .subscribe({
                next: res => {
                    res.datas.rows.forEach(item => {
                        item['isSelect'] = this.isSelect;
                    });
                    this.list = res.datas.rows;
                    complete();
                },
                complete: () => this.loading = false
            });
    }

    //获取合同详情
    loadDetailDatas(detailId: number) {
        this.idDetail = detailId;
        this.manageService.getGood(this.idDetail)
            .subscribe({
                next: res => {
                    this.detailList = res.datas.goodsMsg;
                    this.list.forEach((v, i) => {
                        if (v.id === this.idDetail) {
                            this.list[i].isSelect = true;
                        } else {
                            this.list[i].isSelect = false;
                        }
                    });

                },
                complete: () => this.loading = false
            });
    }

    //删除
    confirmDelete(goods: Goods) {
        this.confirm.danger('删除确认', `您确认删除商品'${goods.goodsName}'吗？`)
            .pipe(switchMap<void, ApiData>(() => this.manageService.deleteGoods(goods.id)))
            .subscribe(() => {
                this.list.splice(this.list.indexOf(goods), 1);
                this.toast.success('删除成功', `成功删除商品'${goods.goodsName}`);
                this.loadDatas();
            });
    }
}
